package pers.li.controller.user;

import pers.li.model.VO.UserVO;
import pers.li.service.UserService;
import pers.li.utils.PublicUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/user/updatePersonalInfo")
public class UpdatePersonalInfo extends HttpServlet {
    private final UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO userVO = PublicUtils.getObjectByRequest(UserVO.class, req);
        assert userVO != null;
        userService.updateUser(userVO);
        Map<String,Object> login = (Map<String,Object>) req.getSession().getAttribute("login");
        UserVO user = (UserVO)login.get("user");
        user.setUserAge(userVO.getUserAge());
        user.setUserEmail(userVO.getUserEmail());
        user.setUserSex(userVO.getUserSex());
        resp.sendRedirect(req.getContextPath() + "/user/user_center.jsp");
    }
}
